package hypeerweb;

import communications.GlobalObjectId;
import communications.LocalObjectId;
import communications.MachineAddress;
import communications.ObjectDB;
import communications.PeerCommunicator;
import communications.PortNumber;

public class HyPeerWebServer {
	
	private static String getCommandLineArgument(String targetArg, String[] args)
	{
		targetArg = "-" + targetArg;
		
		for (int i = 0; i < args.length; ++i) {
			boolean portParam = args[i].equals(targetArg);
			boolean hasAnotherParam = (i + 1) < args.length;
			if (portParam && hasAnotherParam)
			{
				return args[i+1];
			}
		}
		
		return null;
	}
	
	private static PortNumber getPort(String[] args)
	{	
		String result = getCommandLineArgument("p", args);

		if (result != null)
		{
			try
			{
				return new PortNumber(Integer.parseInt(result));
			}
			catch (NumberFormatException e)
			{
				System.err.println(result + " is not a valid port number");
			}
		}
		
		return PortNumber.DEFAULT_PORT_NUMBER;
	}
	
	private static HyPeerWebProxy getParentSegment(String[] args)
	{	
		String address = getCommandLineArgument("parent", args);

		if (address != null)
		{
			String[] splitAddress = address.split(":");
			
			PortNumber port = PortNumber.DEFAULT_PORT_NUMBER;
			String machineAddress = splitAddress[0];
			
			if (splitAddress.length == 2)
			{
				try
				{
					port = new PortNumber(Integer.parseInt(splitAddress[1]));
				}
				catch (NumberFormatException e)
				{
					System.err.println(splitAddress[1] + " is not a valid port number");
				}
			}
			
			if (machineAddress.equals("localhost"))
			{
				machineAddress = MachineAddress.getThisMachinesInetAddress().getHostAddress();
			}
			
			System.out.println("Parent segment on: " + machineAddress + ":" + port);
			GlobalObjectId globalId = new GlobalObjectId(machineAddress, port, new LocalObjectId(HyPeerWeb.HYPEER_WEB_LOCAL_ID));
			return new HyPeerWebProxy(globalId);
		}
		
		return null;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		ObjectDB.setFileLocation("Database.db");
		ObjectDB.getSingleton().clear();
		
		LocalObjectId.setNextId(HyPeerWeb.HYPEER_WEB_LOCAL_ID);
		
		ObjectDB.getSingleton().store(new LocalObjectId(), HyPeerWeb.getSingleton());
		
		HyPeerWebProxy parentSegment = getParentSegment(args);
		
        System.out.println("Server::main(String[]) ObjectDB = ");
        ObjectDB.getSingleton().dump();
		try{
			PortNumber port = getPort(args);
			System.out.println("Starting hypeerweb segment on port " + port);
			System.out.println("==========================================================================================================");
			
			HyPeerWeb.getSingleton().reload("HyPeerWeb_" + port.getValue() + ".db");
			
			PeerCommunicator.createPeerCommunicator(port);

			if (parentSegment != null)
			{
				HyPeerWeb.getSingleton().setParentSegment(parentSegment);
			}
		}catch(Exception e){
			System.err.println(e.getMessage());
			System.err.println(e.getStackTrace());
		}
	}

}
